# -*-coding:utf-8-*-

from scrapy import log

"""避免被ban策略之一：使用useragent池。 

使用注意：需在settings.py中进行相应的设置。 
"""

import random
from scrapy.contrib.downloadermiddleware.useragent import UserAgentMiddleware


class RotateUserAgentMiddleware(UserAgentMiddleware):

    def process_request(self, request, spider):
        request.headers.setdefault(b'User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
    #     ua = random.choice(self.user_agent_list)
    #     if ua:
    #         # 显示当前使用的useragent
    #         print("********Current UserAgent:%s************" % ua)
    #
    #         # 记录
    #         request.headers.setdefault('User-Agent', ua)
    #
    #         # the default user_agent_list composes chrome,I E,firefox,Mozilla,opera,netscape
    #
    # # for more user agent strings,you can find it in http://www.useragentstring.com/pages/useragentstring.php
    # user_agent_list = ["Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13"]
    #
